import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import '../../page/login/login.dart';
import '../../routes/route.dart';
import '../../widget/common_widget.dart';

class ResponseData<T> {
  bool success;
  int code;
  String msg;
  String? token;
  T? data;
  String? path;
  int? total;
  T? rows;

  ResponseData(this.success, this.code, this.msg, this.data, {this.path,this.total,this.token,this.rows});

  static ResponseData<T> createFrom<T>(ResponseData responseData, T data) {
    if (responseData.success == false) {
      //http错误
      showToast("服务器连接失败，请稍后重试！${responseData.code}");
    } else if (responseData.code != 200) {
      if (responseData.msg != null && responseData.msg.isNotEmpty && responseData.code != -101) {
        showToast(responseData.msg);
      }else if(responseData.code == 401) {
        SmartDialog.dismiss();
        Navigator.pushAndRemoveUntil(MyRoute.navigatorKey.currentContext!, PageRouteBuilder(
            settings: const RouteSettings(name: "login"),
            pageBuilder:
                (BuildContext context, Animation animation,
                Animation secondaryAnimation) {
              return const LoginPage();
            }), (route) => false);
      }
    }

    return  ResponseData(
      responseData.success,
      responseData.code,
      responseData.msg,
      data,
      path: responseData.path,
      total: responseData.total,
      token: responseData.token,
      rows: responseData.rows
    );
  }
}
